package com.hai.prism.simple;

import com.hai.prism.driver.CH341;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;

public class SimpleTest {
	
    private static CH341 CH341 = null;
    
    /**
     * 加载CH341动态库API，实例化CH341
     * */
    static {
        String bits = System.getProperty("sun.arch.data.model");// 获取jdk位数
        System.out.println("jdk位数\t" + bits);
        String ops = System.getProperty("os.name");   // 获取os名称
        System.out.println("操作系统：\t" + ops);
        if (ops.startsWith("win") || ops.startsWith("Win")){ // windows
            if ("32".equals(bits)) {
                System.out.println("程序加载了:\tCH341DLLA.dll");
                System.load(System.getProperty("user.dir") + "\\lib\\ch341\\CH341DLLA64.DLL");
                CH341 = (CH341) Native.loadLibrary("CH341DLLA", CH341.class);
 
            } else {
                System.out.println("程序加载了:\tCH341DLLA64.dll");
                System.load(System.getProperty("user.dir") + "\\lib\\ch341\\CH341DLLA64.DLL");
                CH341 = (CH341) Native.loadLibrary("CH341DLLA64", CH341.class);
            }
 
        } else {//其他操作系统 //去WSC官方下载CH341对应操作系统驱动即可支持本地操作系统
            System.out.println("当前仅支持Windows系统，暂不支持其他操作系统！");
        }
    }

	public static void main(String[] args) throws InterruptedException {
		
		CH341.CH341OpenDevice(0);
		
		
		Pointer callBackData = new Memory(1);
		CH341.CH341ReadI2C(0, (byte)0x39, (byte)0x80, callBackData);
		System.out.println(callBackData.getByte(0));
		
		byte writeData = (byte) 0x1;
		CH341.CH341WriteI2C(0, (byte)0x39, (byte)0x80, writeData);
		CH341.CH341ReadI2C(0, (byte)0x39, (byte)0x80, callBackData);
		System.out.println(callBackData.getByte(0));
		
		writeData = (byte) (callBackData.getByte(0) | (1<<1));
		CH341.CH341WriteI2C(0, (byte)0x39, (byte)0x80, writeData);
		CH341.CH341ReadI2C(0, (byte)0x39, (byte)0x80, callBackData);
		System.out.println(callBackData.getByte(0));
		
		writeData = (byte) (callBackData.getByte(0) | (1<<4));
		CH341.CH341WriteI2C(0, (byte)0x39, (byte)0x80, writeData);
		CH341.CH341ReadI2C(0, (byte)0x39, (byte)0x80, callBackData);
		System.out.println(callBackData.getByte(0));
		
		CH341.CH341WriteI2C(0, (byte)0x39, (byte)0x80, (byte)0x01);
		CH341.CH341WriteI2C(0, (byte)0x39, (byte)0xB2, (byte)0x10);
		CH341.CH341WriteI2C(0, (byte)0x39, (byte)0xF9, (byte)0x01);
		CH341.CH341WriteI2C(0, (byte)0x39, (byte)0xAF, (byte)0x10);
		CH341.CH341WriteI2C(0, (byte)0x39, (byte)0x00, (byte)0x30);
		CH341.CH341WriteI2C(0, (byte)0x39, (byte)0x01, (byte)0x01);
		CH341.CH341WriteI2C(0, (byte)0x39, (byte)0x02, (byte)0x00);
		CH341.CH341WriteI2C(0, (byte)0x39, (byte)0x03, (byte)0x00);
		CH341.CH341WriteI2C(0, (byte)0x39, (byte)0x04, (byte)0x00);
		CH341.CH341WriteI2C(0, (byte)0x39, (byte)0x05, (byte)0x42);
		CH341.CH341WriteI2C(0, (byte)0x39, (byte)0x06, (byte)0x00);
		CH341.CH341WriteI2C(0, (byte)0x39, (byte)0x08, (byte)0x50);
		CH341.CH341WriteI2C(0, (byte)0x39, (byte)0x09, (byte)0x00);
		CH341.CH341WriteI2C(0, (byte)0x39, (byte)0x0A, (byte)0x00);
		CH341.CH341WriteI2C(0, (byte)0x39, (byte)0x0B, (byte)0x00);
		CH341.CH341WriteI2C(0, (byte)0x39, (byte)0x0C, (byte)0x20);
		
		boolean status = false;
		while(!status){
			Thread.sleep(10);
			CH341.CH341ReadI2C(0, (byte)0x39, (byte)0xA3, callBackData);
			byte statusData = callBackData.getByte(0);
			System.out.println(statusData);
			if ((statusData & (1 << 6)) == 0) {
				status = true;
			} else {
				status = false;
			}
		 }
		
		writeData = (byte) 0x0;
		CH341.CH341WriteI2C(0, (byte)0x39, (byte)0x80, writeData);
		Native.free(Pointer.nativeValue(callBackData));//手动释放内存
		Pointer.nativeValue(callBackData, 0);//避免Memory对象被GC时重复执行Nativ.free()方法
		CH341.CH341CloseDevice(0);
		
	}

}
